package com.example.ceshi;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.ceshi.SQLite.MySQLiteOpenHelper;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_usr,et_pwd,et_ensure;
    private MySQLiteOpenHelper mySQLiteOpenHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        findViewById(R.id.btn_login).setOnClickListener(this);
        findViewById(R.id.btn_submit).setOnClickListener(this);
        et_usr = findViewById(R.id.register_usr_et);
        et_pwd = findViewById(R.id.register_pwd_et);
        et_ensure = findViewById(R.id.register_ensurePwd_et);

        mySQLiteOpenHelper = new MySQLiteOpenHelper(RegisterActivity.this);
    }

    @Override
    public void onClick(View view) {
        Intent intent=new Intent();
        if(view.getId()==R.id.btn_login){
//            返回登录界面
            intent.setClass(this,LoginActivity.class);
            startActivity(intent);
        }else if(view.getId()==R.id.btn_submit){
//            提交注册信息
            //判断两次密码是否输入正确
            String usr = et_usr.getText().toString();
            String pwd = et_pwd.getText().toString();
            String ensure = et_ensure.getText().toString();
            if(!pwd.equals(ensure)){
                et_pwd.clearComposingText();
                et_ensure.clearComposingText();
                Toast.makeText(this,"两次密码输入不相同，请重新输入！",Toast.LENGTH_SHORT).show();
            }
            //数据库中插入注册用户信息
            else{
                if(!mySQLiteOpenHelper.selectUserByUsername(usr)){
                        Toast.makeText(this,"该用户已存在！",Toast.LENGTH_SHORT).show();
                }else{
                    mySQLiteOpenHelper.insertUserData(usr,pwd);
                    Toast.makeText(this,"注册成功！",Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}